home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Containrs / sa / a_stack < prev    next >
Text File  |  1997-03-08  |  3KB  |  135 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- a_stack.sa: Array based stack
  3. -- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
  4. -- Copyright (C) 1995, International Computer Science Institute
  5. -- $Id: a_stack.sa,v 1.6 1996/07/16 04:38:09 holger Exp $
  6. --
  7. -- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
  8. -- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
  9. -- LICENSE contained in the file: Sather/Doc/License of the
  10. -- Sather distribution. The license is also available from ICSI,
  11. -- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
  12. -------------------------------------------------------------------
  13. --!!! THIS FILE HAS BEEN CREATED FROM a_stack.sa, DO NOT EDIT IT !!!
  14. class STACK{T} < $STACK{T} is include A_STACK{T} end; --NR:
  15. ---NR: class NR_STACK{T} < $NR_STACK{T} is include NR_A_STACK{T} end;
  16. -------------------------------------------------------------------
  17.  
  18. class A_STACK{T} < $STACK{T}  --NR: class NR_A_STACK{T} < $NR_STACK{T}
  19.   is
  20.    -- An array-based stack implemented by delegation to an FLIST{T},
  21.    -- which allocates space by amortized doubling.
  22.    
  23.    
  24.  
  25.    private attr s: FLIST{T};
  26.    
  27.    create: SAME is
  28.       res ::= new;
  29.       
  30.       res.s := #FLIST{T};
  31.       return(res);
  32.    end;
  33.  
  34.    create(e: $ELT{T}): SAME is
  35.       -- Push the elements of "e" onto the stack.
  36.       -- If "e" is an ordered collection this will push elements
  37.       -- such that the last element is left at the top of the stack
  38.       res ::= #SAME;
  39.       loop res.push(e.elt!) end;
  40.       return res;
  41.    end;
  42.  
  43.    create_from(a: ARRAY{T}): SAME is
  44.       return create(a);
  45.    end;
  46.       
  47.    create_capacity(n: INT): SAME is
  48.       -- Preallocate n empty elements 
  49.       res ::= new;
  50.       res.s := #FLIST{T}(n);
  51.       
  52.       return(res);
  53.    end;
  54.    
  55.    remove:T is
  56.  
  57.          return pop; 
  58.       
  59.    end;
  60.    
  61.    current: T is 
  62.  
  63.      return top ;
  64.       
  65.    end;
  66.    
  67.    push(e: T) pre ~void(self)  is
  68.  
  69.      s := s.push(e);
  70.       
  71.    end;
  72.    
  73.    pop: T pre ~void(self) and ~is_empty is 
  74.  
  75.      return(s.pop);
  76.       
  77.    end;
  78.    
  79.    top: T pre ~void(self) and ~is_empty is
  80.       -- Return the  top element of the stack
  81.  
  82.      return(s.top);
  83.       
  84.    end;
  85.    
  86.    reverse_elt!: T pre ~void(self) is
  87.       -- Yield the elements of the stack in reverse order i.e.
  88.       -- ending with "top". 
  89.  
  90.      loop yield(s.elt!) end;
  91.       
  92.    end;
  93.  
  94.    elt!: T pre ~void(self) is
  95.  
  96.          loop yield s[(s.size-1).downto!(0)]; end;
  97.       
  98.    end;
  99.    
  100.    top!: T pre ~void(self) is
  101.       -- Same as elt!
  102.  
  103.          loop yield s[(s.size-1).downto!(0)]; end;
  104.       
  105.    end;
  106.  
  107.  
  108.    size: INT pre ~void(self) is return(s.size) end;
  109.    
  110.    is_empty: BOOL pre ~void(self) is return(size = 0) end;
  111.    
  112.    has(e: T): BOOL pre ~void(self) is
  113.  
  114.      return(s.has(e));
  115.       
  116.    end;
  117.    
  118.    str: STR is
  119.  
  120.      return s.str;
  121.       
  122.    end;
  123.  
  124.    copy: SAME pre ~void(self) is --NR:
  125.    --NR: copy: SAME pre ~void(self) is
  126.  
  127.      res ::= create_capacity(size);
  128.      res.s := s.copy;
  129.      return res;
  130.       
  131.    end;
  132.    
  133. end;
  134. -------------------------------------------------------------------   
  135.